/**
 * KopsoxApplicationServices
 * Copyright (C) 2010 Free Software Foundation, Inc. <http://fsf.org/>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kopsox.services.security;

import java.io.Serializable;
import java.security.Principal;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * Represents a group of Principals (users). A group is unique by its name
 * 
 * @author Konrad Renner
 *
 */
public final class PrincipalGroup implements Set<Principal>, Principal, Serializable {
	
	/**
	 * serialVersionUID = -1904540906768889968L
	 */
	private static final long serialVersionUID = -1904540906768889968L;
	
	private final Set<Principal> users;
	private final String name;
	private final int hashCode;
	
	public PrincipalGroup(String nameOfGroup) {
		users = new HashSet<Principal>();
		name = nameOfGroup;
		hashCode = 31 * 17 + name.hashCode();
	}

	/* (non-Javadoc)
	 * @see java.security.Principal#getName()
	 */
	@Override
	public String getName() {
		return name;
	}

	@Override
	public boolean add(Principal object) {
		return users.add(object);
	}

	@Override
	public boolean addAll(Collection<? extends Principal> collection) {
		return users.addAll(collection);
	}

	@Override
	public void clear() {
		users.clear();
	}

	@Override
	public boolean contains(Object object) {
		return users.contains(object);
	}

	@Override
	public boolean containsAll(Collection<?> collection) {
		return users.containsAll(collection);
	}

	@Override
	public boolean isEmpty() {
		return users.isEmpty();
	}

	@Override
	public Iterator<Principal> iterator() {
		return users.iterator();
	}

	@Override
	public boolean remove(Object object) {
		return users.remove(object);
	}

	@Override
	public boolean removeAll(Collection<?> collection) {
		return users.removeAll(collection);
	}

	@Override
	public boolean retainAll(Collection<?> collection) {
		return users.retainAll(collection);
	}

	@Override
	public int size() {
		return users.size();
	}

	@Override
	public Object[] toArray() {
		return users.toArray();
	}

	@Override
	public <T> T[] toArray(T[] array) {
		return users.toArray(array);
	}

	@Override
	public int hashCode() {
		return hashCode;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PrincipalGroup other = (PrincipalGroup) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	@Override
	public String toString() {
		
		StringBuilder sb = new StringBuilder();
		sb.append("PrincipalGroup [users=");
		sb.append(users);
		sb.append(", name=");
		sb.append(name);
		sb.append(']');
		
		return sb.toString();
	}
	
}
